﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;

namespace Hinacom.Scheduler
{
    /// <summary>
    /// 任务执行时所需的参数。
    /// </summary>
    public class JobParameter: IDictionary<string, object>, ICollection<KeyValuePair<string, object>>
    {
        public JobParameter()
        {
            dir = new Dictionary<string, object>();
        }
        public JobParameter(IDictionary<string, object> source): this()
        {
            this.SetData(source);
        }
        /// <summary>
        /// 将指定IDictionary对象中的值复制到该对象中。
        /// </summary>
        /// <param name="source">复制源。</param>
        public void SetData(IDictionary<string, object> source)
        {
            if (source != null)
            {
                foreach (string key in source.Keys)
                    this.dir[key] = source[key];
            }
        }

        private Dictionary<string, object> dir;

        #region 接口实现
        /// <summary>
        /// 获取或设置指定的值。
        /// </summary>
        /// <param name="key">值的标识。</param>
        /// <returns>获取的值。</returns>
        public object this[string key]
        {
            get
            {
                if (dir.ContainsKey(key))
                    return dir[key];
                return null;
            }
            set
            {
                if (dir.ContainsKey(key))
                    dir[key] = value;
                else
                    dir.Add(key, value);
            }
        }

        [Browsable(false)]
        public int Count
        {
            get
            {
                return dir.Count;
            }
        }
        [Browsable(false)]
        public ICollection<string> Keys
        {
            get
            {
                return dir.Keys;
            }
        }
        [Browsable(false)]
        public ICollection<object> Values
        {
            get
            {
                return dir.Values;
            }
        }

        public void Add(string key, object value)
        {
            dir.Add(key, value);
        }

        public void Clear()
        {
            dir.Clear();
        }

        public bool ContainsKey(string key)
        {
            return dir.ContainsKey(key);
        }

        public IEnumerator<KeyValuePair<string, object>> GetEnumerator()
        {
            return dir.GetEnumerator();
        }

        public bool Remove(string key)
        {
            return dir.Remove(key);
        }

        public bool TryGetValue(string key, out object value)
        {
            return dir.TryGetValue(key, out value);
        }

        IEnumerator IEnumerable.GetEnumerator()
        {
            return dir.GetEnumerator();
        }

        bool ICollection<KeyValuePair<string, object>>.IsReadOnly
        {
            get
            {
                return false;
            }
        }

        void ICollection<KeyValuePair<string, object>>.Add(KeyValuePair<string, object> item)
        {
            this.Add(item.Key, item.Value);
        }

        bool ICollection<KeyValuePair<string, object>>.Contains(KeyValuePair<string, object> item)
        {
            return this.dir.Contains(item);
        }

        void ICollection<KeyValuePair<string, object>>.CopyTo(KeyValuePair<string, object>[] array, int arrayIndex)
        {
            throw new NotImplementedException();
        }

        bool ICollection<KeyValuePair<string, object>>.Remove(KeyValuePair<string, object> item)
        {
            return this.Remove(item.Key);
        }
        #endregion
    }
}
